<?php
//-- Checking link
if(!$_GET['link'])
	$link=".";
else
	$link .=$_GET['link'];
//-- reading directory
if($opendir=opendir($link)){
        while(($files_or_dir=readdir($opendir))!==false){
            if($files_or_dir!="."&&$files_or_dir!="..")
            {
            $all_items[]=$files_or_dir;
            if(is_dir($link."/".$files_or_dir))
            $tr_dir .="<tr> <td><input type='checkbox' name='dir[]' value='".$files_or_dir."'/></td><td><a href='?link=".$link."/".$files_or_dir."'>".$files_or_dir."</a></td></tr>";
            elseif(is_file($link."/".$files_or_dir))
            $tr_file .= "<tr> <td><input type='checkbox' name='files[]' value='".$files_or_dir."'/></td><td>".$files_or_dir."</td></tr>";
            }
            
	}
 }
 
 if($_GET[act]=="Delete")
 {
    //--  Deleting File 
    if($_GET['files'])
    {
       
       foreach($_GET['files'] as $del)
        {
          // print_r($link."/".$del); 
           unlink($link."/".$del);
        } 
        header("Location: ?link=$_GET[link]");
    }
    else
    $msg .="Nothing Selected";
  
  //-- deleting folder
if($_GET['dir']){     
     foreach( $_GET['dir'] as $del)
     {
     $msg .="Not Work";
     } 
    }
}
//-- File edit
if($_GET[act]=="Edit"){
    // set file to read
    foreach($_GET['files'] as $file){
        $filename=$link."/".$file;
    if(is_file($filename)){
        $newdata = $_POST['newd'];
        if ($newdata != '') {
        // open file 
        $fw = fopen($filename, 'w') or die('Could not open file!');
        // write to file
        // added stripslashes to $newdata
        $fb = fwrite($fw,stripslashes($newdata)) or die('Could not write 
        to file');
        // close file
        fclose($fw);
        header("Location: ?link=$_GET[link]");
        }
        // open file
          $fh = fopen($filename, "r");
        // read file contents
          $data = htmlentities(fread($fh, filesize($filename)));
        // close file
          fclose($fh);
        // print file contents
        $msg .="<h3>Contents of File</h3>
        <form action='$_SERVER[php_self]' method= 'post' >
        <textarea name='newd' rows='20'>$data</textarea><br/>
        <input type='submit' value='Change'>
        </form>";  
    }
    if(is_dir($filename))
    $msg .= "Select File";
    }
    
}
//-- rename 
 if($_GET[act]=="Rename"){
    if($_GET['dir']!='' || $_GET['files']!=''){
        if($_GET['dir'])
        $name=$_GET['dir'];
        else
        $name=$_GET['files'];
        foreach($name as $file){
            $filename=$link."/".$file;
            $msg .="<h3>File Name</h3>
            <form action='$_SERVER[php_self]' method= 'post' >
            <input type='text' name='newname' value='$file'/><br/>
            <input type='submit' value='Rename'>
            </form>";
           if($_POST[newname]!=''){
            if(in_array($_POST[newname],$all_items)){
                $msg .= "exist";
           }
            else
            rename($filename , ($link."/".$_POST[newname]));
            header("Location: ?link=$_GET[link]");
           } 
           
        }
    }
    else $msg="Select File or Folder";
}
//-- make dir -----------------------------------------------------------
if($_GET[act]=="New Folder"){
        
        $msg .="<h3>Folder Name</h3>
        <form action='$_SERVER[php_self]' method= 'post' >
        <input type='text' name='new_folder_name'/><br/>
        <input type='submit' value='New Folder'>
        </form>"; 
        if($_POST[new_folder_name]!=''){
        if(in_array($_POST[new_folder_name],$all_items)){
            $msg .="Already exist";
       }
        else
        mkdir($link."/".$_POST[new_folder_name]);
        header("Location: ?link=$_GET[link]");
       } 
    
}
//-- New File-----------------------------------------------------------
if($_GET[act]=="New File"){
        
       $msg .="<h3>File Name</h3>
        <form action='$_SERVER[php_self]' method= 'post' >
        <input type='text' name='new_file_name'/><br/>
        <input type='submit' value='New File'>
        </form>"; 

      if($_POST[new_file_name]!=''){
        if(in_array($_POST[new_file_name],$all_items)){
            $msg .="already exist";
       }
        else
        fopen(($link."/".$_POST[new_file_name]),'w');
        fclose($link."/".$_POST[new_file_name]);
       } 
       
    
}
 //-- Folder links

$back="<a href='?link=.'>root</a>";
$dir_link=explode("/",$_GET['link']);
$x_path .=".";
foreach($dir_link as $path)
{
    if($path!="." && $path!=".."){
        $back .=" / <a href='?link=".$x_path."'>".$path."</a>";
        $x_path .="$path";
    }
}


//print_r($_GET);
?>
<!doctype html>
<html>
<head>
	<title>Joy</title>
	<link rel="stylesheet" type="text/css" href="css/style.css" />
    <script src="js/jquery.js"></script>
    <script src="js/jquery-ui.js"></script>
   	<script src="js/functions.js"></script>
</head>

<body>
	<div id="main_body">
 <form action="" method="get">
 <table >
 <tr>
 <td><input type="submit" name="act" value="Delete"/></td>
 <td><input type="submit" name="act" value="Edit"/></td>
 <td><input type="submit" name="act" value="New File"/></td>
 <td><input type="submit" name="act" value="New Folder"/></td>
 <td><input type="submit" name="act" value="Rename"/></td>
 <td></td>
 </tr>
 <tr><td>Link : </td><td><a href="#">&larr;</a> </td><td colspan="4"><?php echo $back; ?></td></tr>
 
 </table>
 <hr />
  <table>
 <input type="hidden" name="link" value="<?php echo $_GET['link'] ?>" /> 

	<?php 
    
     echo $tr_dir; 
     echo $tr_file;  
    ?>
     
    </table>
</form>
<div id="msg_body">
    <div id="msg">
    <div class="msg"><?php echo $msg?></div>
    <div id="close"><button>Close</button></div>
    </div>
</div>
	</div>
</body>
</html>